<?php

/**
 * @copyright Copyright &copy; Kartik Visweswaran, Krajee.com, 2014 - 2015
 * @package yii2-money
 * @version 1.2.1
 */

namespace backend\widgets\input4Query;

use Yii;
use yii\helpers\Html;
use yii\widgets\InputWidget;
use kartik\dialog\Dialog;
use yii\web\JsExpression;

/**
 * Captcha renders a CAPTCHA image and an input field that takes user-entered verification code.
 *
 * Captcha is used together with [[CaptchaAction]] to provide [CAPTCHA](http://en.wikipedia.org/wiki/Captcha) - a way
 * of preventing website spamming.
 *
 * The image element rendered by Captcha will display a CAPTCHA image generated by
 * an action whose route is specified by [[captchaAction]]. This action must be an instance of [[CaptchaAction]].
 *
 * When the user clicks on the CAPTCHA image, it will cause the CAPTCHA image
 * to be refreshed with a new CAPTCHA.
 *
 * You may use [[\yii\captcha\CaptchaValidator]] to validate the user input matches
 * the current CAPTCHA verification code.
 *
 * The following example shows how to use this widget with a model attribute:
 *
 * ```php
 * echo Captcha::widget([
 *     'model' => $model,
 *     'attribute' => 'captcha',
 * ]);
 * ```
 *
 * The following example will use the name property instead:
 *
 * ```php
 * echo Captcha::widget([
 *     'name' => 'captcha',
 * ]);
 * ```
 *
 * You can also use this widget in an [[yii\widgets\ActiveForm|ActiveForm]] using the [[yii\widgets\ActiveField::widget()|widget()]]
 * method, for example like this:
 *
 * ```php
 * <?= $form->field($model, 'captcha')->widget(\yii\captcha\Captcha::classname(), [
 *     // configure additional widget properties here
 * ]) ?>
 * ```
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class Input4Query extends InputWidget
{
    /**
     * @inherit doc
     */
    /**
     * @var string the template for arranging the CAPTCHA image tag and the text input tag.
     * In this template, the token `{image}` will be replaced with the actual image tag,
     * while `{input}` will be replaced with the text input tag.
     */

    public $template = '{input} {search}';

    public $options = ['class' => 'form-control','style' => 'float:left;margin-right:-2px;'];

    /**
     * @var string 跳转页面
     */
    public $redirectUrl = '';

    /**
     * @var string 自定义的Wedgit ID;
     */
    public $libName = '';

    public $inputName = '';
    /**
     * @var string 返回值的回调函数;
     */
    public $backFunction = 'successCallback';

    /**
     * @var string 清空的回调函数, 如此函数未定义则只清空对应的Input 控件本身;
     */
    public $trashFunction = 'trashCallback';

    /**
     * @var string 对话框Title;
     */
    public $dialogTitles = '';
    /**
     * @inherit doc
     */
    public function init()
    {
        parent::init();
    }

    public function run()
    {

        // 输出基本弹出框;
        echo Dialog::widget([
            'libName' => $this->libName, // a custom lib name
            'options' => [  // customized BootstrapDialog options
                'size' => Dialog::SIZE_LARGE, // large dialog text
                'type' => Dialog::TYPE_PRIMARY, // bootstrap contextual color
                'title' => $this->dialogTitles,
                'closable' => false,
                'options' => ['realized' => true, 'closable' => false],
                'message' => 'This is an entirely customized',
                'buttons' => [
                    [
                        // OK 按钮及事件
                        'id' => 'cust-btn-ok',
                        'label' => Yii::t('backend', 'OK'),
                        'icon' => Dialog::ICON_OK,
                        'class' => 'btn btn-success',
                        'action' => new JsExpression("function(dialog) {
                            // 取返回值,如为空则停留在此窗口;
                            var _dialogReturnValue = dialog.getButton('cust-btn-ok').attr('attrs');
                             alert(_dialogReturnValue);
                            //开始调用加调函数;
                            ".$this->backFunction."('$this->inputName',_dialogReturnValue);
                               
                            if(typeof(_dialogReturnValue)=='undefined'){
                                return false;
                            }else{
                                //重置查询按键的显示属性, 显示删除按钮;
                                $('#".$this->name."' + '_trash').removeClass('hidden');
                            }
                           dialog.close();
                        }")
                    ],
                    [
                        'id' => 'cust-btn-5',
                        'label' => Yii::t('backend', 'Cancel'),
                        'icon' => Dialog::ICON_CANCEL,
                        'action' => new JsExpression("function(dialog) { 
                    dialog.close();
                }")
                    ],

                ]
            ]
        ]);

        // $this->registerClientScript();

        if ($this->hasModel()) {
            $input = Html::activeTextInput($this->model, $this->attribute, $this->options);
        } else {
            $input = Html::textInput($this->name, $this->value, $this->options);
        }

        $_search = Html::a('<i id="'. $this->name .'_search" class="search fa fa-search"></i><i id="'. $this->name .'_trash" class="trash hidden fa fa-trash"></i>',"javascript:void('1');",['id'=>$this->name]);
        echo strtr($this->template, [
            '{input}' => $input,
            '{search}' => $_search,
        ]);
        $_gotoUrl = $this->createIframeUrl();
        $_trashFunc = $this->trashFunction;
        $_input = $this->inputName;
        $js = <<< JS
        
        $("#$this->name" + "_trash").on("click", function() {
            if(confirm("您确定要删除吗? ")){
                if(typeof $this->trashFunction === 'function'){
                    $_trashFunc('$_input');
                }else{
                }
            }
        });
        
        $("#$this->name" + "_search").on("click", function() {
            $this->libName.dialog(
                '$_gotoUrl',
                function(result) {
                }
            );
        });
JS;

        // register javascript
        $view = $this->getView();
        $view->registerJs($js);
    }

    /**
     * @param $_url
     * @return string
     * 创建Iframe 框架, 局部刷新及提交;
     */
    public function createIframeUrl(){
        $_iframurl = "<iframe width=\'80%\' height=\'60%\' value=\'0\' class=\'iframeDialog\' id=\'qqqqqq\' frameborder=\'0\' scrolling=\'\' src=\'".$this->redirectUrl."\'>Loading...</iframe>";
        return $_iframurl;
    }
}
